package com.iscc.fake.controller;

import com.google.gson.Gson;
import com.iscc.fake.domain.Form;
import com.iscc.fake.service.FormService;
import com.iscc.fake.service.Impl.FormServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet({"/AddFormServlet.do"})
public class AddFormServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> resultMap = new HashMap<>();
        Gson gson =new Gson();
        FormService FormService =new FormServiceImpl();
        //创建DiskFileItemFactory对象,允许文件上传；
        DiskFileItemFactory factory=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(factory);
        Form form=new Form();
        List<DiskFileItem> dlist =new ArrayList<>();
        try {
            List<FileItem> items = upload.parseRequest(request);
            for(FileItem item:items){
                if(item.isFormField()){
                    String val =new String(item.getString().getBytes("iso-8859-1"),"UTF-8");
                    if("date".equals(item.getFieldName())){
                        form.setDate(val);
                    }else if ("uno".equals(item.getFieldName())){
                        form.setUno(Integer.parseInt(val));
                    }else if ("pprice".equals(item.getFieldName())){
                        form.setPprice(Double.parseDouble(val));
                    }else if ("text".equals(item.getFieldName())){
                        form.setText(val);
                    }else if ("status".equals(item.getFieldName())){
                        form.setStatus(Integer.parseInt(val));
                    }else if ("addno".equals(item.getFieldName())){
                        form.setAddno(Integer.parseInt(val));
                    }
                }
                }
            int result = FormService.addForm(form);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "添加订单成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "添加订单失败");
            }
            response.getWriter().print(gson.toJson(resultMap));
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
